package Controllers;

import DB.*;
import Models.*;
import java.util.ArrayList;

public class SearchController
{
	public ArrayList<Customer> searchCustomer(String searchText)
	{
		DBCustomer dbc = new DBCustomer();
		String[] columns = Customer.getSearchColumns();
		return dbc.fetchAll(createWhereClause(columns, searchText), "name", 0);
	}

	public ArrayList<Location> searchLocation(String searchText)
	{
		DBLocation dbl = new DBLocation();
		String[] columns = Location.getSearchColumns();
		return dbl.fetchAll(createWhereClause(columns, searchText), "zipCode", 0);
	}

	public ArrayList<Route> searchRoute(String searchText)
	{
		DBRoute dbr = new DBRoute();
		String[] columns = Route.getSearchColumns();
		return dbr.fetchAll(createWhereClause(columns, searchText), "routeNo", 0);
	}

	public ArrayList<Round> searchRound(String searchText)
	{
		DBRound dbr = new DBRound();
		String[] columns = Round.getSearchColumns();
		return dbr.fetchAll(createWhereClause(columns, searchText), "locationNo", 0);
	}

	private String createWhereClause(String[] columns, String searchText)
	{
		String whereClause = "";
		int i = 1;
		for (String column : columns)
		{
			whereClause += column+" LIKE '%"+searchText+"%' ";
			if(i != columns.length)
			{
				whereClause += "OR ";
			}
			i++;
		}
		return whereClause;
	}
}